package com.ningxia.water.controller;


import com.ningxia.water.enums.JobStatusEnum;
import com.ningxia.water.entity.JobTask;
import com.ningxia.water.reuslt.Result;
import com.ningxia.water.reuslt.ResultResponse;
import com.ningxia.water.service.IJobTaskService;
import com.ningxia.water.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author hz
 * @since 2021-05-19
 */
@RestController
@RequestMapping("/job")
@Api(tags = "计划任务-定时任务管理")
public class JobTaskController {

    @Autowired
    private IJobTaskService jobTaskService;

    @GetMapping("/getalljobtasks")
    @ApiOperation(value = "获取所有任务列表")
    public Result getAllJobTasks() {
        return ResultResponse.getSuccessResult(jobTaskService.list());
    }

    @PostMapping("/insertjobtask")
    @ApiOperation(value = "添加定时任务")
    public Result insertJobTask(@RequestBody JobTask jobTask) {
        return CommonUtils.commonResult(jobTaskService.save(jobTask));
    }

    @DeleteMapping("/deletejobtask")
    @ApiOperation(value = "通过id删除定时任务")
    public Result deleteJobTask(@RequestParam("id") Integer id) {
        JobTask jobTask = jobTaskService.getById(id);
        if (jobTask != null && JobStatusEnum.RUNNING.getCode().equals(jobTask.getJobStatus())) {
            return ResultResponse.getFailResult("删除前请先停止任务");
        }
        return CommonUtils.commonResult(jobTaskService.removeJobTask(id));
    }


    @PutMapping("/updatejobtask")
    @ApiOperation(value = "修改定时任务")
    public Result updateJobTask(@RequestBody JobTask jobTask) {
        if (JobStatusEnum.RUNNING.getCode().equals(jobTask.getJobStatus())) {
            return ResultResponse.getFailResult("修改前请先停止任务");
        }
        return CommonUtils.commonResult(jobTaskService.updateById(jobTask));
    }

    @PutMapping("/changejobstatus")
    @ApiOperation(value = "修改任务状态")
    public Result changeJobStatus(@RequestParam("id") Integer id, @RequestParam("jobStatus") Integer jobStatus) {
        String status = jobStatus == 1 ? JobStatusEnum.RUNNING.getCode() : JobStatusEnum.STOP.getCode();
        try {
            return CommonUtils.commonResult(jobTaskService.changeJobStatus(id, status));
        } catch (SchedulerException e) {
            return ResultResponse.getFailResult("更新任务状态失败");
        }
    }

    @PutMapping("/updatecron")
    @ApiOperation("修改cron表达式")
    public Result updateCronExpression(@RequestParam("jobId") Integer jobId, @RequestParam("cronExpression") String cronExpression) {
        JobTask jobTask = jobTaskService.getById(jobId);
        if (jobTask != null && JobStatusEnum.RUNNING.getCode().equals(jobTask.getJobStatus())) {
            return ResultResponse.getFailResult("修改Cron表达式前请先停止任务");
        }
        try {
            return CommonUtils.commonResult(jobTaskService.updateCronExpression(jobId, cronExpression));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return ResultResponse.getFailResult("修改cron表达式失败");
    }


//    @PostMapping("/runjob")
//    @ApiOperation("运行任务-无用，因为修改任务状态的时候就直接启动了")
//    public Result runJob(@RequestParam("id") Integer id) {
//        JobTask jobtask = jobTaskService.getById(id);
//        try {
//            if (jobtask == null) {
//                return ResultResponse.getFailResult("没有此任务");
//            }
//            if (JobStatusEnum.STOP.getCode().equals(jobtask.getJobStatus())) {
//                return ResultResponse.getFailResult("请先开启任务");
//            } else {
//                jobTaskService.run(jobtask);
//                return ResultResponse.getSuccessResult();
//            }
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//
//        return ResultResponse.getFailResult("执行任务失败");
//    }


}
